package sg.edu.nus.iss.cats.action;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import sg.edu.nus.iss.cats.controller.*;
import sg.edu.nus.iss.cats.exception.*;

public class CourseApproveAction extends Action {

    private static final String APPROVE = "approve";
    private static final String REJECT  = "reject";

	public ActionForward execute(ActionMapping mapping,
								 ActionForm form,
								 HttpServletRequest request,
								 HttpServletResponse response)
						 throws CourseNotFoundException, CourseUpdateException {

		System.out.println("CourseApproveAction");

		String courseId = request.getParameter("course_id");
		String comment  = request.getParameter("comment");
		String decision = request.getParameter("decision");
		System.out.println("CourseApproveAction- courseId: "+courseId);
		System.out.println("CourseApproveAction- comment: "+comment);
		System.out.println("CourseApproveAction- decision: "+decision);

		// Get the modifier id (current user's employees id)

		HttpSession session = request.getSession();
		UserSession userSession =
			(UserSession) session.getAttribute("USERSESSION");
		String modifierId = userSession.getEmployee().getEmployeeId();

		CourseManager courseMgr = new CourseManager();

        if (APPROVE.equals(decision)) {
		    System.out.println("CourseApproveAction: approving");
		    courseMgr.approveCourse(courseId, modifierId, comment);
		} else if (REJECT.equals(decision)) {
		    System.out.println("CourseApproveAction: rejecting");
		    courseMgr.rejectCourse(courseId, modifierId, comment);
		}

		// If no exception was thrown, go to the default page

		System.out.println("CourseApproveAction successful");
		return mapping.findForward("managerDefault");
	}
}